package com.briup.srms.exam.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.briup.srms.exam.bean.*;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.briup.srms.exam.bean.extend.ExamAnswerExtend;
import com.briup.srms.exam.bean.extend.StudentExamExtend;
import com.briup.srms.exam.dao.ExamAnswerMapper;
import com.briup.srms.exam.dao.ExamMapper;
import com.briup.srms.exam.dao.QuestionMapper;
import com.briup.srms.exam.dao.StudentExamMapper;
import com.briup.srms.exam.service.IAnswerService;
import com.briup.srms.exam.service.IPaperQuestionService;
import com.jacky.base.utils.CustomerException;

@Service
public class AnswerServiceImpl implements IAnswerService {

	@Autowired
	private ExamAnswerMapper examAnswerMapper;
	@Autowired
	private StudentExamMapper seMapper;
	@Autowired
	private QuestionMapper questionMapper;
	@Autowired
	private IPaperQuestionService paperQuestionService;
	@Autowired
	private ExamMapper examMapper;

	@Override
	public List<ExamAnswer> queryByStudentExamId(long studentExamId) {
		ExamAnswerExample example = new ExamAnswerExample();
		example.createCriteria().andStudentExamIdEqualTo(studentExamId);
		return examAnswerMapper.selectByExample(example);
	}

	@Override
	@Transactional
	public void submitPaperAnswer(List<ExamAnswerExtend> list) {
		// TODO Auto-generated method stub
		if(list.size()== 0) {
			throw new CustomerException("参数不合法");
		}
		// 获取学生考试
		Long seId = list.get(0).getStudentExamId();
		StudentExam se = seMapper.selectByPrimaryKey(seId);
		if(se == null) {
			throw new CustomerException("学生考试信息不存在");
		}
		if(!se.getStatus().equals( "未参加")) {
			throw new CustomerException("学生已经提交过本次答卷");
		}

		// 获取考试信息
		Exam exam = examMapper.selectByPrimaryKey(se.getExamId());
		// 插入答卷信息
		Double score_keguan = 0.0D;
		for(ExamAnswerExtend answer : list) {
			// 客观题分数
			Double score = this.getScore(answer);
			score_keguan += score;
			// 存储多选题答案
			if(answer.getAnswers() !=null && answer.getAnswers().size()>0) {
				answer.setAnswer(StringUtils.join(answer.getAnswers(), ','));
			}
			examAnswerMapper.insert(answer);
		}
		// 修改主观题成绩
		se.setScoreSubjective(0.0D);
		// 修改客观题成绩
		se.setScoreObjective(score_keguan);
		// 修改学生考试
		se.setScore(score_keguan);
		// 设置考试状态
		if(exam.getIsinspection() == 1) {
			se.setStatus(StudentExamExtend.STATUS_COMPLETE);	// 考试结束
		} else {
			se.setStatus(StudentExamExtend.STATUS_FINISH);	// 考试完成
		}

		seMapper.updateByPrimaryKey(se);
	}

	// 获取分数
	@SuppressWarnings("unchecked")
	@Transactional
	public Double getScore(ExamAnswerExtend answer) {
        Double score = 0.0D;
		// 查询问题答案
		Question question = questionMapper.selectByPrimaryKey(answer.getQuestionId());

		// 成绩在paper_question中
		PaperQuestion pq = paperQuestionService.queryByPaperAndQuestionId(answer.getPaperId(), answer.getQuestionId());
		Double score_question = pq.getScore();
		switch (question.getType()) {
		case "单选":
			// 查询正确答案
			if(question.getAnswer().equals(answer.getAnswer())) {
				score = score_question;
			}
			break;
		case "多选":
			// 输入答案
			List<String> a_input_arr = answer.getAnswers();
			// 正确答案
			String a_correnct = question.getAnswer();
			String[] a_correct_arr = a_correnct.split(",");
			List<String> a_correct_list = CollectionUtils.arrayToList(a_correct_arr);
			// 全部正确
			int correct_num = 0;
			for(String a : a_input_arr) {
				if(!a_correct_list.contains(a)) {
					correct_num = -1;
					break;
				} else {
					correct_num++;
				}
			}

			if(correct_num ==a_correct_list.size() ) {
				score = score_question;
			} else if(correct_num<a_correct_list.size() &&  correct_num>0) {
				BigDecimal bd = new BigDecimal(score_question);
				// 四舍五入
				score = Double.valueOf(Math.round(Double.parseDouble( bd.divide(new BigDecimal(2)).toString())));
			}
			break;
		case "判断":
			// 查询正确答案
			if(question.getIsCorrect() == Integer.parseInt(answer.getAnswer())) {
				score = score_question;
			}
			break;
		default:
			break;
		}



		return score;
	}

	@Override
	@Transactional
	public void inspectionPaper(List<ExamAnswer> answers) throws CustomerException {
		// 获取学生考试
		Long seId = answers.get(0).getStudentExamId();
		StudentExam se = seMapper.selectByPrimaryKey(seId);
		if(se == null) {
			throw new CustomerException("学生考试信息不存在");
		}
		// 插入答卷信息
		Double scoreSubjective = 0.0D;
		for(ExamAnswer answer : answers) {
			scoreSubjective += answer.getScore();
			// 更新主观题成绩
			examAnswerMapper.updateByPrimaryKey(answer);
		}
		// 更新主观题成绩
		se.setScoreSubjective(scoreSubjective);
		// 更新总成绩
		se.setScore(se.getScoreObjective()+se.getScoreSubjective());
		se.setStatus(StudentExamExtend.STATUS_FINISH);	// 考试结束
		seMapper.updateByPrimaryKey(se);
	}

}
